/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.gl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Glaccnt;
import src.domain.gl.Tax;
import src.services.gl.GlaccntConverter;
import src.services.gl.GlaccntModel;
import src.services.gl.TaxModel;
import src.util.Messager;

/**
 *
 * @author jay
 */

@ManagedBean
@SessionScoped
public class TaxBean implements Serializable {

    private List<Tax> taxs;
    //chan private List<SelectItem> availableGL;
    private List<Glaccnt> availableGL;
    private Glaccnt tesgl;
    private Glaccnt unsaveDataGl;
    private Tax selectedData, unsaveData;
    private GlaccntModel glHelper;
    private TaxModel helper;
    private boolean editMode=true;

    public TaxBean() {
        this.helper = new TaxModel();
        this.glHelper = new GlaccntModel();
        this.taxs = helper.findAll(null);
        this.availableGL = GlaccntConverter.glaccountDB;// glHelper.findAll(null); //chan listAll();
        this.selectedData = null;
        this.unsaveData = null;
    }

    public List<Tax> getTaxs() {
        return this.taxs;
    }
    
    public Tax getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Tax selectedData) {
        this.selectedData = selectedData;
    }

    public Glaccnt getUnsaveDataGl() {
        return unsaveDataGl;
    }

    public void setUnsaveDataGl(Glaccnt unsaveDataGl) {
        this.unsaveDataGl = unsaveDataGl;
    }

    public Tax getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(Tax unsaveData) {
        this.unsaveData = unsaveData;
    }
        
    public List<Glaccnt> getAvailableGL() {
//       if (availableGL != null) return availableGL;
//       
//       availableGL = new ArrayList<>();
//       Glaccnt gl = new Glaccnt();
//       gl.setGlaccntId(93);
//       gl.setAccountName("coba");
//       gl.setAccountNo("00");
       
        return availableGL;

    }
   
    public String getGLName(Integer id) {
        String nama = "";
        if (id!=null && !id.equals(0)) {
            int idx=0;
            boolean found=false;
            while (idx<availableGL.size() && !found) {
                //chan SelectedItem it = availableGL.get(idx);
                Glaccnt it = availableGL.get(idx);
                //chan found = id.equals(it.getValue()) ? true:false;
                found = id.equals(it.getGlaccntId()) ? true:false;
                //chan if (found) nama = it.getLabel();
                if (found) nama = it.getAccountName();
                idx++;
            }
        }
        return nama;
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null;
    }
    
    public void initForm() {
        this.availableGL = GlaccntConverter.glaccountDB;//glHelper.findAll(null); //chan listAll();
    }
    
    public void openData() { 
        initForm();
        this.unsaveData = Tax.clone(selectedData);
        this.unsaveDataGl = glHelper.getById(this.unsaveData.getGlaccountId());
        this.editMode = true;
    }
    
    public void newData() {  
        initForm();
        this.selectedData = null;
        this.unsaveData = new Tax();
        this.unsaveDataGl = new Glaccnt();
        this.editMode = false;
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
        this.unsaveData.setGlaccountId(this.unsaveDataGl.getGlaccntId());
        boolean success = helper.saveData(this.unsaveData);
        if (success==true) {
            if (this.editMode==false) this.taxs.add(unsaveData);
            else {
                int i = taxs.indexOf(selectedData);
                if (i <= taxs.size()) taxs.set(i, Tax.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setTaxId(null);
        } 
                        
        Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.taxs.remove(this.selectedData);
            this.selectedData = null;
        }
               
        Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }

    
    
    
}
